﻿using HandyControl.Controls;
using HardToolApp.Model;
using HardToolApp.Tool;
using HardToolApp.View;
using NLua;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HardToolApp.ViewModel
{
    [ViewModelHelper("IIC从机", typeof(IICSlavePage), typeof(IICSlaveModel), Sort = 4)]
    internal class IICSlaveVM : CommVM
    {
        public IICSlaveVM() 
        {
            ScriptVM.register("iic_slave_set", "(从机地址)", "IIC_Slave", this.GetType(), "IIC从机设置", "0:操作成功,1:操作失败");
            ScriptVM.register("iic_slave_send", "(发送数据,超时时间)", "IIC_Slave", this.GetType(), "IIC从机数据发送", "0:操作成功,1:操作失败");
            ScriptVM.register("iic_slave_send_byte", "(发送数据:byte数组,超时时间)", "IIC_Slave", this.GetType(), "IIC从机数据发送", "0:操作成功,1:操作失败");
            ScriptVM.register("iic_slave_read", "(读取长度,超时时间)", "IIC_Slave", this.GetType(), "IIC从机数据读取", "读取的byte数组");
        }
        // 接收到串口相关
        public override void RecOkData(byte[] b)
        {
            switch (b[4])
            {
                case (byte)CommSubCmdIICSlave.COMM_SUB_IIC_SLAVE_READ://数据接收
                    ((IICSlaveModel)Model).receiveBytes = Common.Byte2String(b.Skip(6).ToArray(), len: b[2] - 7);
                    USBInfoVM.USBVm.AddMap((b[3] << 8) + b[4], Common.String2Byte(((IICSlaveModel)Model).receiveBytes));
                    break;
                default:
                    USBInfoVM.USBVm.AddMap((b[3] << 8) + b[4], 0);
                    break;
            }
                
            
        }
        public static object iic_slave_set(byte addr)
        {
            byte[] bytes = new byte[7];
            bytes[0] = 0x55;
            bytes[1] = 0xaa;
            bytes[2] = 7;
            bytes[3] = (byte)CommCmd.COMM_IIC_SLAVE;
            bytes[4] = (byte)CommSubCmdIICSlave.COMM_SUB_IIC_SLAVE_SET_PARAM;
            bytes[5] = addr;
            bytes[6] = Common.chksum(bytes);
            return USBInfoVM.USBVm.send(bytes);
        }
        public static object iic_slave_send(string sendBytes,UInt16 timeout)
        {
            byte[] data = Common.String2Byte(sendBytes);
            byte[] bytes = new byte[data.Count() + 8];
            bytes[0] = 0x55;
            bytes[1] = 0xaa;
            bytes[2] = (byte)bytes.Length;
            bytes[3] = (byte)CommCmd.COMM_IIC_SLAVE;
            bytes[4] = (byte)CommSubCmdIICSlave.COMM_SUB_IIC_SLAVE_WRITE;
            bytes[5] = (byte)(timeout >> 8);
            bytes[6] = (byte)(timeout);
            data.CopyTo(bytes, 7);
            bytes[data.Count() + 7] = Common.chksum(bytes);
            return USBInfoVM.USBVm.send(bytes, (int)timeout + 1000);
        }
        public static object iic_slave_send_byte(LuaTable sendBytes, UInt16 timeout)
        {
            byte[] data = Common.Lua2Byte(sendBytes);
            byte[] bytes = new byte[data.Count() + 8];
            bytes[0] = 0x55;
            bytes[1] = 0xaa;
            bytes[2] = (byte)bytes.Length;
            bytes[3] = (byte)CommCmd.COMM_IIC_SLAVE;
            bytes[4] = (byte)CommSubCmdIICSlave.COMM_SUB_IIC_SLAVE_WRITE;
            bytes[5] = (byte)(timeout >> 8);
            bytes[6] = (byte)(timeout);
            data.CopyTo(bytes, 7);
            bytes[data.Count() + 7] = Common.chksum(bytes);
            return USBInfoVM.USBVm.send(bytes, (int)timeout + 1000);
        }
        public static object iic_slave_read(byte len,UInt16 timeout)
        {
            byte[] bytes = new byte[9];
            bytes[0] = 0x55;
            bytes[1] = 0xaa;
            bytes[2] = 9;
            bytes[3] = (byte)CommCmd.COMM_IIC_SLAVE;
            bytes[4] = (byte)CommSubCmdIICSlave.COMM_SUB_IIC_SLAVE_READ;
            bytes[5] = len;
            bytes[6] = (byte)(timeout >> 8);
            bytes[7] = (byte)(timeout);
            bytes[8] = Common.chksum(bytes);
            return USBInfoVM.USBVm.send(bytes,(int)timeout+1000);
        }
        /// <summary>
        /// 设置SPI参数
        /// </summary>
        /// <param name="m"></param>
        public override void Send(Peripheral t)
        {
            IICSlaveModel m = t as IICSlaveModel;
            if (m.subCmd == (byte)Communicate.SubCmd.SetParam)
            {
                iic_slave_set(m.addr);
            }
            else if (m.subCmd == (byte)Communicate.SubCmd.SendData)
            {
                iic_slave_send(m.sendBytes,m.timeout);
            }
            else if (m.subCmd == (byte)Communicate.SubCmd.ReadData)
            {
                iic_slave_read(m.readLen,m.timeout);
            }
        }

    }
}
